package com.ztf.identity;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.task.IdentityLink;
import org.junit.Test;

public class IdentityTest {
    /**
     *  act_id_group：角色组表
     act_id_user：用户表：
     act_id_membership：用户角色表

     */
    @Test
    public void testCreateUserAndGroup(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        GroupEntity groupEntity = new GroupEntity("咨询组");
        processEngine.getIdentityService().saveGroup(groupEntity);
        UserEntity userEntity1 = new UserEntity("咨询员1");
        UserEntity userEntity2 = new UserEntity("咨询员2");
        UserEntity userEntity3 = new UserEntity("咨询员3");
        processEngine.getIdentityService().saveUser(userEntity1);
        processEngine.getIdentityService().saveUser(userEntity2);
        processEngine.getIdentityService().saveUser(userEntity3);
        processEngine.getIdentityService().createMembership("咨询员1", "咨询组");
        processEngine.getIdentityService().createMembership("咨询员2", "咨询组");
        processEngine.getIdentityService().createMembership("咨询员3", "咨询组");
    }

    @Test
    public void testDeploy(){
        //获取流程引擎
        ProcessEngine processEngine =  ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("com/itheima09/activiti5/identity/identity.bpmn")
                .deploy();
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
                .startProcessInstanceById("identity:1:3504");
    }

    /**
     * 通过taskID获得该任务的组ID和组里面的人
     */
    @Test
    public void testQueryGroupByTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //根据taskID得到该组任务的信息
        List<IdentityLink> identityLinks = processEngine.getTaskService()
                .getIdentityLinksForTask("3604");
        for (IdentityLink identityLink : identityLinks) {
            System.out.println(identityLink.getGroupId());
            List<User> users = processEngine.getIdentityService()
                    .createUserQuery()
                    .memberOfGroup(identityLink.getGroupId())//根据该组任务的组的id得到该组的人
                    .list();
            for (User user : users) {
                System.out.println(user.getId());
            }
        }
    }
}
